package cn.xorange.commons.system.controller.platform;

import cn.xorange.commons.core.basic.bean.PageBean;
import cn.xorange.commons.core.basic.bean.ResultBean;
import cn.xorange.commons.core.basic.controller.BaseController;
import cn.xorange.commons.logs.annotation.Log;
import cn.xorange.commons.logs.enums.BusinessType;
import cn.xorange.commons.security.annotation.Authorize;
import cn.xorange.commons.security.utils.SecurityUtils;
import cn.xorange.commons.system.entity.platform.SysDictData;
import cn.xorange.commons.system.service.platform.ISysDictDataService;
import cn.xorange.commons.system.service.platform.ISysDictTypeService;
import cn.xorange.commons.utils.poi.excel.EasyExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 数据字典信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/platform/dict/data")
@Authorize(hasPerm = "platform:dict")
public class SysDictDataController extends BaseController
{
    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private ISysDictTypeService dictTypeService;

    @Authorize(hasPerm = "list")
    @GetMapping("/list")
    public PageBean list(SysDictData dictData)
    {
        startPage();
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        return new PageBean(list);
    }

    @Log(title = "字典数据", businessType = BusinessType.EXPORT)
    @Authorize(hasPerm = "export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDictData dictData)
    {
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        EasyExcelUtil.exportExcel(list,"字典数据","字典数据", SysDictData.class,"导出_字典数据",response);
    }

    /**
     * 查询字典数据详细
     */
    @Authorize(hasPerm = "query")
    @GetMapping(value = "/{dictCode}")
    public ResultBean getInfo(@PathVariable Long dictCode)
    {
        return ResultBean.onSuccess(dictDataService.selectDictDataById(dictCode));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/type/{dictType}")
    public ResultBean dictType(@PathVariable String dictType)
    {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (Objects.isNull(data))
        {
            data = new ArrayList<>();
        }
        return ResultBean.onSuccess(data);
    }

    /**
     * 新增字典类型
     */
    @Authorize(hasPerm = "add")
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @PostMapping
    public ResultBean add(@Validated @RequestBody SysDictData dict)
    {
        dict.setCreateBy(SecurityUtils.getUsername());
        return ResultBean.onSuccess(dictDataService.insertDictData(dict));
    }

    /**
     * 修改保存字典类型
     */
    @Authorize(hasPerm = "edit")
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResultBean edit(@Validated @RequestBody SysDictData dict)
    {
        dict.setUpdateBy(SecurityUtils.getUsername());
        return ResultBean.onSuccess(dictDataService.updateDictData(dict));
    }

    /**
     * 删除字典类型
     */
    @Authorize(hasPerm = "remove")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictCodes}")
    public ResultBean remove(@PathVariable Long[] dictCodes)
    {
        dictDataService.deleteDictDataByIds(dictCodes);
        return ResultBean.onSuccess();
    }
}
